import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:puppy_store/models/goods_detail_model.dart';
import 'package:puppy_store/pages/goods_detail/goods_detail_address.dart';
import 'package:puppy_store/pages/goods_detail/goods_specs_widget.dart';

// 定义gloablekey
GlobalKey<_GoodsDetailOptionsWidgetState> goodsDetailOptionsKey = GlobalKey(
  debugLabel: 'goodsDetailOptionsKey',
);

class GoodsDetailOptionsWidget extends StatefulWidget {
  const GoodsDetailOptionsWidget({this.goodsDetailModel, super.key});

  final GoodsDetailModel? goodsDetailModel;

  @override
  State<GoodsDetailOptionsWidget> createState() =>
      _GoodsDetailOptionsWidgetState();
}

class _GoodsDetailOptionsWidgetState extends State<GoodsDetailOptionsWidget> {
  String? _specsStr;

  //  收货地址
  UserAddress? _userAddress;

  // 入口公共函数
  showGoodsSpecsCupertinoModalPopup(SpecsActionType type) {
    // 展示由底向上的一个弹窗
    showCupertinoModalPopup(
      context: context,
      builder: (context) {
        return GoodsSpecsWidget(
          type: type,
          goodsDetailModel: widget.goodsDetailModel,
          specsStrCallback: (showSpecsStr) {
            setState(() {
              _specsStr = showSpecsStr ?? '请选择规格数量';
            });
            debugPrint('specsStrCallback: $showSpecsStr');
          },
        );
      },
    );
  }

  void _mockAddress() {
    widget.goodsDetailModel?.userAddresses?.add(
      UserAddress(
        id: "1397791884939628545",
        receiver: "itcast",
        contact: "18500001111",
        // provincecode:"110000",
        // citycode:"110100",
        // countycode:"110114",
        address: "传智教育",
        isDefault: true,
        fullLocation: "北京北京市昌平区",
        // postalcode:null,
        // addressTags:null,
      ),
    );

    widget.goodsDetailModel?.userAddresses?.add(
      UserAddress(
        id: "1397792003437105154",
        receiver: "itheima",
        contact: "18500001111",
        // provincecode:"110000",
        // citycode:"110100",
        // countycode:"110113",
        address: "传智播客",
        isDefault: false,
        fullLocation: "北京北京市顺义区",
        // postalcode:null,
        // addressTags:null,
      ),
    );
  }

  // 遍历用户所有的收货地址,取出默认地址
  // 用户可以没有地址, 如果无地址,则服务端返回的UserAddress字段为空
  _getUserDefaultAddress(List<UserAddress>? userAddresses) {
    userAddresses?.forEach((UserAddress element) {
      if (element.isDefault!) {
        setState(() {
          _userAddress = element;
        });
        return;
      }
    });
  }

  @override
  void initState() {
    // 模拟用户的地址
    // _mockAddress();
    // 遍历用户所有的收货地址,取出默认地址
    _getUserDefaultAddress(widget.goodsDetailModel?.userAddresses);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      padding: EdgeInsets.only(left: 20.0, right: 20.0),
      child: MediaQuery.removePadding(
        context: context,
        removeTop: true,
        removeBottom: true,
        child: ListView(
          shrinkWrap: true,
          physics: NeverScrollableScrollPhysics(),
          children: [
            GestureDetector(
              onTap: () {
                // // 展示由底向上的一个弹窗
                showGoodsSpecsCupertinoModalPopup(SpecsActionType.normal);
                // showCupertinoModalPopup(
                //   context: context,
                //   builder: (context) {
                //     return GoodsSpecsWidget(
                //       goodsDetailModel: widget.goodsDetailModel,
                //       specsStrCallback: (showSpecsStr) {
                //         setState(() {
                //           _specsStr = showSpecsStr ?? '请选择规格数量';
                //         });
                //         debugPrint('specsStrCallback: $showSpecsStr');
                //       },
                //     );
                //   },
                // );
              },
              child: Container(
                height: 44.0,
                alignment: Alignment.center,
                color: Colors.white,
                child: Column(
                  children: [
                    Expanded(
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(
                            '已选',
                            style: TextStyle(
                              fontSize: 13.0,
                              color: Color(0xFF898B94),
                            ),
                          ),
                          // 保证选项撑满剩余的中间的位置
                          Expanded(
                            child: Padding(
                              padding: EdgeInsets.only(left: 10),
                              child: Text(
                                _specsStr ?? '请选择规格数量',
                                style: TextStyle(
                                  fontSize: 13.0,
                                  color: Color(0xFF262626),
                                ),
                                maxLines: 1,
                                overflow: TextOverflow.ellipsis,
                              ),
                            ),
                          ),

                          Padding(
                            padding: EdgeInsets.only(left: 10.0),
                            child: Image.asset('assets/arrow_right.png'),
                          ),
                        ],
                      ),
                    ),
                    Container(height: 1.0, color: Color(0xFFF7F7F8)),
                  ],
                ),
              ),
            ),
            GestureDetector(
              onTap: () {
                showCupertinoModalPopup(
                  context: context,
                  builder: (BuildContext context) {
                    return GoodsDetailAddressWidget(
                      userAddresses: widget.goodsDetailModel?.userAddresses,
                      userAddressCallback: (UserAddress userAddress) {
                        setState(() {
                          _userAddress = userAddress;
                        });
                      },
                    );
                  },
                );
              },
              child: Container(
                height: 44.0,
                alignment: Alignment.center,
                color: Colors.white,
                child: Column(
                  children: [
                    Expanded(
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(
                            '送至',
                            style: TextStyle(
                              fontSize: 13.0,
                              color: Color(0xFF898B94),
                            ),
                          ),
                          // 保证选项撑满剩余的中间的位置
                          Expanded(
                            child: Padding(
                              padding: EdgeInsets.only(left: 10),
                              child: Text(
                                _userAddress != null
                                    ? "${_userAddress!.fullLocation!} ${_userAddress!.address!}"
                                    : '请选择收货地址',
                                style: TextStyle(
                                  fontSize: 13.0,
                                  color: Color(0xFF262626),
                                ),
                                maxLines: 1,
                                overflow: TextOverflow.ellipsis,
                              ),
                            ),
                          ),

                          Padding(
                            padding: EdgeInsets.only(left: 10.0),
                            child: Image.asset('assets/arrow_right.png'),
                          ),
                        ],
                      ),
                    ),
                    Container(height: 1.0, color: Color(0xFFF7F7F8)),
                  ],
                ),
              ),
            ),
            GestureDetector(
              onTap: () {
                debugPrint('点击了服务');
              },
              child: Container(
                height: 44.0,
                alignment: Alignment.center,
                color: Colors.white,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      '服务',
                      style: TextStyle(
                        fontSize: 13.0,
                        color: Color(0xFF898B94),
                      ),
                    ),
                    // 保证选项撑满剩余的中间的位置
                    Expanded(
                      child: Padding(
                        padding: EdgeInsets.only(left: 10),
                        child: Text(
                          '无忧退 货快速退款 免费包邮',
                          style: TextStyle(
                            fontSize: 13.0,
                            color: Color(0xFF262626),
                          ),
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                    ),
                    Padding(
                      padding: EdgeInsets.only(left: 10.0),
                      child: Image.asset('assets/arrow_right.png'),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
